' to find location of installed product

If WScript.Arguments.Count <> 1 Then
	WScript.Echo "Usage: getinst.vbs <application name>"
	WScript.Quit 1
End If

' strComputer = "."
' 
' Set objWMIService = GetObject("winmgmts:" _
' 	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' 
' Set colSoftware = objWMIService.ExecQuery _
' 	("SELECT * FROM Win32_Product")
' 
' ' InstallState:
' '
' ' -6 Bad configuration
' ' 
' ' -2 Invalid argument
' ' 
' ' -1 Unknown package
' ' 
' '  1 Advertised
' '  
' '  2 Absent
' '  
' '  6 Installed
' 
' For Each objSoftware in colSoftware
' 	If Not IsNull(objSoftware.InstallLocation) And _
' 		InStr(objSoftware.Name, WScript.Arguments(0)) Then
' 		WScript.StdOut.WriteLine objSoftware.InstallLocation
' 		WScript.Quit 0
' 	End If
' ' UNDONE: make use of
' ' objSoftware.Caption
' ' objSoftware.Version
' Next

' as in most MICROS~1 ways, the above is rather useless

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")

Const HKEY_LOCAL_MACHINE = &H80000002 ' HKLM
strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
strKeyPath2 = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"

Function EnumerateKeys(hive, key)
	objReg.EnumKey hive, key, arrSubKeys
	If Not IsNull(arrSubKeys) Then
		For Each subkey In arrSubKeys
			EnumerateKeys = EnumerateKeys(hive, key & "\" & subkey)
			If Not IsNull(EnumerateKeys) Then
				Exit Function
			End If
		Next
	Else
		objReg.GetStringValue HKEY_LOCAL_MACHINE, key, "DisplayName", strValue
		If Not IsNull(strValue) And InStr(strValue, WScript.Arguments(0)) Then
			objReg.GetStringValue HKEY_LOCAL_MACHINE, key, "InstallLocation", strValue
			If Not IsNull(strValue) Then
				EnumerateKeys = Replace(strValue, "/", "\")
				Exit Function
			End If
			objReg.GetStringValue HKEY_LOCAL_MACHINE, key, "UninstallString", strValue
      If Left(strValue, 1) = """" And Right(strValue, 1) = """" Then
        strValue = Mid(strValue, 2, Len(strValue) - 2)
      End If
			strFolder = objFSO.GetParentFolderName(strValue)
			If Not IsNull(strFolder) Then
				EnumerateKeys = Replace(strFolder, "/", "\")
				Exit Function
			End If
		End If
	End If
	EnumerateKeys = NULL
End Function

Set objReg = GetObject("winmgmts://./root/default:StdRegProv")

result = EnumerateKeys(HKEY_LOCAL_MACHINE, strKeyPath)

If Not IsNull(result) Then
	WScript.StdOut.WriteLine result
	WScript.Quit 0
End If

result = EnumerateKeys(HKEY_LOCAL_MACHINE, strKeyPath2)

If Not IsNull(result) Then
	WScript.StdOut.WriteLine result
	WScript.Quit 0
End If

' add more options with HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

WScript.Quit 1
